package com.xli.mis.tablecategory.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xli.dto.component.model.TableModel;
import com.xli.dto.controller.BaseController;
import com.xli.dto.params.IdsDTO;
import com.xli.dto.result.ResultVO;
import com.xli.dto.result.status.Status;
import com.xli.dto.validation.group.IGroup;
import com.xli.mis.tablecategory.entity.TableCategory;
import com.xli.mis.tablecategory.entity.dto.TableCategoryAddDTO;
import com.xli.mis.tablecategory.entity.dto.TableCategorySearchDTO;
import com.xli.mis.tablecategory.entity.dto.TableCategoryUpdateDTO;
import com.xli.mis.tablecategory.entity.mapper.ITableCategoryMapper;
import com.xli.mis.tablecategory.entity.vo.TableCategoryVO;
import com.xli.mis.tablecategory.service.ITableCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@Tag(name = "子系统分类")
@RestController
@RequestMapping("/tableCategory")
public class TableCategoryController extends BaseController {

    @Autowired
    ITableCategoryService iTableCategoryService;

    @Operation(summary = "保存分类", description = "保存分类")
    @PostMapping(value = "/add")
    public ResultVO<String> add(@RequestBody @Validated(IGroup.add.class) TableCategoryAddDTO dto) {
        TableCategory tableCategory = iTableCategoryService.findByName(dto.getCategoryName());
        if (tableCategory == null) {
            tableCategory = ITableCategoryMapper.INSTANCE.toEntity(dto);
            tableCategory.setId(IdUtil.getSnowflakeNextId());
            if (iTableCategoryService.insert(tableCategory)) {
                return new ResultVO<>(Status.SUCCESS, "保存成功");
            }
            return new ResultVO<>(Status.FAILED, "保存失败");
        }
        return new ResultVO<>(Status.FAILED, "保存失败，分类名已存在");
    }

    @Operation(summary = "删除分类", description = "删除分类")
    @PostMapping(value = "/delete")
    public ResultVO<String> delete(@RequestBody @Validated(IGroup.delete.class) IdsDTO dto) {
        int i = 0;
        for (String id : dto.getIds()) {
            if (iTableCategoryService.delete(Long.valueOf(id))) {
                i++;
            }
        }
        return new ResultVO<>(Status.SUCCESS, i + "条数据删除成功");
    }

    @Operation(summary = "更新分类", description = "更新分类")
    @PostMapping(value = "/update")
    public ResultVO<String> update(@RequestBody @Validated(IGroup.update.class) TableCategoryUpdateDTO dto) {
        TableCategory tableCategory = iTableCategoryService.findByName(dto.getCategoryName());
        if (tableCategory == null || dto.getId().equals(tableCategory.getId().toString())) {
            if (iTableCategoryService.update(ITableCategoryMapper.INSTANCE.toEntity(dto))) {
                return new ResultVO<>(Status.SUCCESS, "更新成功");
            }
            return new ResultVO<>(Status.FAILED, "更新失败");
        }
        return new ResultVO<>(Status.FAILED, "更新失败，分类名已存在");
    }

    @Operation(summary = "查询分类", description = "查询分类")
    @PostMapping(value = "/search")
    public ResultVO<TableModel<TableCategoryVO, TableCategory, TableCategorySearchDTO>> search(@RequestBody @Validated(IGroup.search.class) TableCategorySearchDTO dto) {

        TableModel<TableCategoryVO, TableCategory, TableCategorySearchDTO> tableModel = new TableModel<>(dto) {
            @Override
            public List<TableCategoryVO> fetch(int current, int pageSize) {
                Page<TableCategory> page = iTableCategoryService.findList(getQueryWrapper(), current, pageSize);
                this.setTotal(page.getTotal());
                return page.convert(ITableCategoryMapper.INSTANCE::toVO).getRecords();
            }
        };
        return new ResultVO<>(Status.SUCCESS, "查询成功", tableModel);
    }
}
